import json
import urllib.request
import urllib.parse
from urllib.request import Request


# url = "https://httpbin.org/get?name='111'"
#
# response = urllib.request.urlopen(url)
# result = response.read().decode("utf-8")
# print(result)
# result_json = json.loads(result)
# print(result_json)


# url = "https://httpbin.org/get?name='111'"
# req = Request(url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}, method='GET')
#
# response = urllib.request.urlopen(req)
# result = response.read().decode("utf-8")
# result_json = json.loads(result)
# print(result_json["headers"]["User-Agent"],result_json["args"])
#


url = "https://httpbin.org/post"
data = {
    "name": "test",
    "age": 18
}
# 转换为 URL 编码的字符串（bytes 类型）
data = urllib.parse.urlencode(data).encode("utf-8")
req = Request(url,data=data, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Content-Type": "application/json"},method="POST")

response = urllib.request.urlopen(req)
if response.getcode() == 200:
    resp = json.loads(response.read().decode("utf-8"))
    print(resp)



